<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%@ taglib uri="/permission" prefix="pe" %>

<jsp:include page="../../include/top.jsp"/>
<div class="main_body">
    <div class="span9 page_message">
        <section id="contents">
            <ul class="tab-nav nav">
                <li class="current"><a href="">活动列表<span class="arrow fa fa-sort-up"></span></a></li>
                <li><a href="javascript:toAdd()">添加/修改<span class="arrow fa fa-sort-up"></span></a></li>
                <li style="background-color:darkorange"><a href="javascript:multiDel()">批量删除<span class="arrow fa fa-sort-up"></span></a></li>
            </ul>
            <p class="normal_tips" style="display:none"><b class="fa fa-info-circle"></b></p>

            <div class="table-bar" style="display: none">
                <div class="search-form fr cf">
                    <form id="myForm" name="myForm" action="${BasePath}/app/activity/list?${PublicParams}" method="post" class="form-inline">
                        <%--用户名：<input type="text" name="name" value="${name }"/>--%>
                        <%--昵称：<input type="text" name="nick" value="${nick}"/>--%>
                        <%--<input type="button" value="查询" onclick="submitForm()" class="btn btn-success " style="margin-top: -10px"/>--%>
                        <input type="hidden" name="start" id="start" value="${start }"/>
                        <input type="hidden" name="limit" id="limit" value="${limit }"/>
                    </form>
                </div>
            </div>
            <!-- 数据列表 -->
            <div class="data-table">
                <div class="table-striped">
                    <table cellspacing="1">
                        <!-- 表头 -->
                        <thead>
                        <tr>
                            <th class="row-selected row-selected">
                                <input type="checkbox" id="checkAll" class="check-all regular-checkbox"><label for="checkAll"></label>
                            </th>
                            <th>ID</th>
                            <th>标题</th>
                            <th>图片</th>
                            <th>跳转链接</th>
                            <th>参与人数</th>
                            <th>时间</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <!-- 列表 -->
                        <tbody>
                        <c:forEach items="${list}" var="obj">
                        <tr>
                            <td><input type="checkbox" id="check_${obj.id}" name="ids[]" value="${obj.id}" class="ids regular-checkbox"><label for="check_${obj.id}"></label></td>
                            <td class="uid">${obj.id}</td>
                            <td>${obj.title}</td>
                            <td><img src="${obj.image}" width="80px" height="80px"></td>
                            <td>${obj.link}</td>
                            <td>${obj.userCount}</td>
                            <td>
                                <script>formatDate('${obj.startDate}');</script>
                                ~
                                <script>formatDate('${obj.endDate}');</script>
                            </td>
                            <td>
                                <c:if test="${obj.status==0}">
                                    <span style="color: green">已上线</span>
                                </c:if>
                                <c:if test="${obj.status==1}">
                                    <span style="color: red">已下线</span>
                                </c:if>
                            </td>
                            <td>
                                <pe:permission key="update">
                                    <a class="btn btn-primary btn-small" href="${BasePath}/app/activity/toEdit?id=${obj.id}&${PublicParams}">修改</a>
                                    <c:if test="${obj.status==1}">
                                        <a class="btn btn-info btn-small" href="javascript:updateStatus('${obj.id}',0)">上线</a>
                                    </c:if>
                                    <c:if test="${obj.status==0}">
                                        <a class="btn btn-warning btn-small" href="javascript:updateStatus('${obj.id}',1)">下线</a>
                                    </c:if>
                                </pe:permission>
                                <pe:permission key="delete">
                                    <a class="btn btn-danger btn-small" type="button" onclick="multiDel('${obj.id}')">删除</a>
                                </pe:permission>
                            </td>
                        </tr>
                        </c:forEach>
                    </table>
                </div>
            </div>
            <div class="page" id="pager"></div>
            <script type="text/javascript">
                var sign = 0;
                window.onload = function () {
                    PAGER.initPager('${start}', ' ${total}', '${limit}', 8, 'pager', getData);
                }
                function getData(currentPage, pageSize) {
                    if (sign > 0) {
                        $("#start").val(currentPage);
                        $("#limit").val(pageSize);
                        $("#myForm").submit();
                    }
                    sign = 1;
                }
            </script>
        </section>
    </div>
</div>

<jsp:include page="../../include/bottom.jsp"/>


<script>
    function toAdd() {
        var idArray = new Array();
        $("input[name='ids[]']:checked").each(function () {
            var txt = $(this).val();
            idArray.push(txt);
        });
        //新增
        if (idArray.length == 0) {
            window.location.href = " ${BasePath}/app/activity/toAdd?${PublicParams}";
        } else if (idArray.length == 1) {
            window.location.href = " ${BasePath}/app/activity/toEdit?${PublicParams}&id=" + idArray;
        } else {
            layer.msg('请选择一条记录进行操作！', {icon: 5});
            return false;
        }
    }
    function multiDel(id) {
        var idArray = new Array();
        if (id != null && id > 0) {
            idArray.push(id);
        } else {
            $("input[name='ids[]']:checked").each(function () {
                var txt = $(this).val();
                idArray.push(txt);
            });
        }
        if (idArray.length == 0) {
            layer.msg('请选择一条记录进行操作！', {icon: 5});
            return false;
        } else {
            layer.msg('你确定要删除吗？', {
                time: 0 //不自动关闭
                , btn: ['确定', '取消']
                , yes: function (index) {
                    layer.close(index);
                    $.getJSON("${BasePath}/app/activity/multiDel?ids=" + idArray.toString(), function (data) {
                        if (data.errno == 0) {
                            layer.msg("操作成功！");
                            $("#myForm").submit();
                        } else {
                            layer.msg(data.errdesc, {icon: 5});
                        }
                    });
                }
            });
        }
    }

    function updateStatus(id, status) {
        layer.msg('确定要操作吗？', {
            time: 0 //不自动关闭
            , btn: ['确定', '取消']
            , yes: function (index) {
                layer.close(index);
                $.getJSON("${BasePath}/app/activity/updateStatus?id="+id+"&status="+status, function (data) {
                    if (data.errno == 0) {
                        layer.msg("操作成功！");
                        $("#myForm").submit();
                    } else {
                        layer.msg(data.errdesc, {icon: 5});
                    }
                });
            }
        });
    }

    function submitForm() {
        $("#start").val(1);
        $("#myForm").submit();
    }
</script>